import Vue from "vue";
import VueRouter from "vue-router";
import pathArr from '@/router/pathArr.js'

// 导入需要的组件
import Login from '@/components/MyLogin.vue'
import Home from '@/components/MyHome.vue'
import MyGoods from '@/components/menus/MyGoods.vue'
import MyOrders from '@/components/menus/MyOrders.vue'
import MyRights from '@/components/menus/MyRights.vue'
import MySettings from '@/components/menus/MySettings.vue'
import MyUsers from '@/components/menus/MyUsers.vue'
import MyUserDetail from '@/components/user/MyUserDetail.vue'

Vue.use(VueRouter)

const router = new VueRouter({
  routes: [
    {path: '/', redirect: '/login'},
    {path: '/login', component: Login},
    {
      path: '/home', 
      component: Home,
      redirect: '/home/users',
      children: [
        {path: 'goods', component: MyGoods},
        {path: 'orders', component: MyOrders},
        {path: 'rights', component: MyRights},
        {path: 'settings', component: MySettings},
        {path: 'users', component: MyUsers},
        // 用户管理的详情信息
        {path: 'userdetail/:mid', component: MyUserDetail, props: true}
      ]
    }
    

  ]
})

// 增加全局前置守卫
router.beforeEach((to, from, next) => {
  if (pathArr.indexOf(to.path) !== -1) {
    const token = localStorage.getItem('token')
   if (token && token.length !== 0) {
      /// 表示token存在
      next()
   } else {
      // 跳转到登录页面
      next('/login')
   }
  } else {
    next()
  }
   
})

export default router